// import {Message} from 'element-ui';
import router from '../components/router/index'
import {showFullScreenLoading, hideFullScreenLoading} from './loding'

// 同步Get请求方法
export function HttpGetSync(url, param) {
    jQuery.support.cors = true;
    let _tokenID = '';
    if (window.sessionStorage.getItem('TokenID')) {
        _tokenID = window.sessionStorage.getItem('TokenID');
    }
    $.ajax({
        url: url,
        data: param,
        crossDomain: true,
        type: "GET",
        async: false,
        dataType: "json",
        timeout: 10000, //超时时间设置，单位毫秒
        beforeSend: function (request) {
            request.setRequestHeader("TokenID", _tokenID);
        },
        success: function (data) {
            return data;
        },
        error: function (data) {
            console.log('服务器执行失败' + data.responseText);
            return '';
        }
    });
}

// 同步Post请求方法
export function HttpPostSync(url, param) {
    let _rd = '';
    jQuery.support.cors = true;
    let _tokenID = '';
    if (window.sessionStorage.getItem('TokenID')) {
        _tokenID = window.sessionStorage.getItem('TokenID');
    }
    $.ajax({
        url: url,
        data: param,
        crossDomain: true,
        type: "POST",
        async: false,
        dataType: "json",
        timeout: 10000, //超时时间设置，单位毫秒
        beforeSend: function (request) {
            request.setRequestHeader("TokenID", _tokenID);
        },
        success: function (data) {
            _rd = data;
        },
        error: function (data) {
            console.log('服务器执行失败' + data.responseText);
        }
    });
    return _rd;
}

//异步Get请求
export function HttpGet(url, param, callback) {
    jQuery.support.cors = true;
    let _tokenID = '';
    if (window.sessionStorage.getItem('TokenID')) {
        _tokenID = window.sessionStorage.getItem('TokenID');
    }
    $.ajax({
        url: url,
        data: param,
        crossDomain: true,
        type: "GET",
        dataType: "json",
        timeout: 10000, //超时时间设置，单位毫秒
        beforeSend: function (request) {
            request.setRequestHeader("Access-Control-Allow-Origin", "*"); //设置跨域访问信息
            request.setRequestHeader("Token", _tokenID);
        },
        success: function (data) {
            callback(data)
        },
        error: function (data) {
            console.log('服务器执行失败[' + url + ']' + data.responseText);
            callback('');
        }
    });
}
//异步Get请求(导出)
export function HttpExport(url, param, callback) {
    jQuery.support.cors = true;
    let _tokenID = '';
    if (window.sessionStorage.getItem('TokenID')) {
        _tokenID = window.sessionStorage.getItem('TokenID');
    }
    $.ajax({
        url: url,
        data: param,
        // crossDomain: true,
        responseType:'blob',
        headers:{
            'Content-Type':'application/json; charset=utf-8',
          },
        type: "POST",
        // dataType: "json",
        timeout: 10000, //超时时间设置，单位毫秒
        beforeSend: function (request) {
            request.setRequestHeader("Access-Control-Allow-Origin", "*"); //设置跨域访问信息
            request.setRequestHeader("Token", _tokenID);
        },
        success: function (data) {
            callback(data)
        },
        error: function (data) {
            console.log('服务器执行失败[' + url + ']' + data.responseText);
            callback('');
        }
    });
}
//异步post请求
export function HttpPost(url, param, callback) {
    showFullScreenLoading();
    jQuery.support.cors = true;
    let _tokenID = '';
    if (window.sessionStorage.getItem('TokenID')) {
        _tokenID = window.sessionStorage.getItem('TokenID');
    }
    $.ajax({
        url: url,
        data: param,
        crossDomain: true,
        type: "POST",
        dataType: "json",
        timeout: 10000, //超时时间设置，单位毫秒
        beforeSend: function (request) {
            request.setRequestHeader("Token", _tokenID);
            request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        },
        success: function (data) {
            hideFullScreenLoading();
            console.log(router.currentRoute.path)
            // if (router.currentRoute.path !== '/login') {
            //     Message({
            //         type: 'error',
            //         duration: 1000,
            //         showClose: true,
            //         message: '登录失效，请重新登录'
            //     });
            //     router.replace({
            //         path: '/login',
            //         query: {
            //             redirect: router.currentRoute.fullPath
            //         }
            //     })
            // }
            callback(data)
        },
        error: function (data) {
            hideFullScreenLoading();
            console.log('服务器执行失败[' + url + ']' + data.responseText);
            callback('');
        }
    });
}

export default {
    HttpGetSync,
    HttpPostSync,
    HttpGet,
    HttpPost,
    HttpExport
}
